<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'layout' => false,
    /* 模块 */
    'modules' => [
        /**
         * 这里我们用Module实现版本v1和v2
         */
        'v1' => [
            'class' => 'api\modules\v1\Module',
            'defaultRoute' => 'site',
        ],
        'v2' => [
            'class' => 'api\modules\v2\Module',
            'defaultRoute' => 'index'
        ],
    ],
    'components' => [
        'user' => [
            'class' => 'yii\web\User',
            'identityClass' => 'api\models\User',
            'enableAutoLogin' => true, //开启自动登录
            'enableSession' => false, //关闭session
            'loginUrl' => null, //登录跳转地址为空
        ],
        //修改默认的request组件
        'request' => [
            'baseUrl' => Yii::getAlias('@apiUrl'),
            'enableCookieValidation' => false,
        ],
        //修改默认的response组件
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                $data = $response->data;
                if ($data !== null && is_array($data)) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $data,
                    ];
                    $response->statusCode = 200;
                    $response->format = \yii\web\Response::FORMAT_JSON;
                }
            },
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false, // 是否启用严格解析，只有在rules中存在的才解析，开启容易出错
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['v1/default', 'v1/user', 'v1/vendue', 'v1/consignee'],
                    //'only' => ['index', 'view', 'delete'], //仅只支持该2个默认方法
                    'pluralize' => false, //是否启用复数形式，注意index的复数indices，我认为开启后不直观
                ]
            ],
        ],

    ],
    'params' => $params,
];
